import {logout} from "./auth"

function createHandler(fun) {
    return function (...args) {
        /**
         * 模拟前后端交互的序列化
         */
        args = JSON.parse(JSON.stringify(args))
        const resp = JSON.parse(JSON.stringify(fun(...args)))
        const {code, msg, data} = resp
        return new Promise((resolve, reject) => {
            if (code !== 0) {
                if (code === 101) {
                    logout()
                }
                reject({code, msg})
            } else {
                resolve(data)
            }
        })
    }
}

export default createHandler